package com.pic.mgt.haishin.executer;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.time.DateUtils;

import com.pic.mgt.haishin.AppointedHaishinRuleConfig;
import com.pic.mgt.haishin.AppointedHaishinSizePattern;
import com.pic.mgt.manager.terminal.BasicTerminalInfoBean;
import com.pic.mgt.utils.DateConvertUtils;

public class ClientTypeExecutionTimeAsgmtFileProcess extends AsgmtFileProcess {
	private static final String EMPTY_CLIENT_TYPE_MAP_KEY = "EMPTY_CLIENT_TYPE";
	
	private AppointedHaishinRuleConfig appointedHaishinRuleConfig;
	private Map<String, String> clientTypeExecutionTimeMap = new HashMap<String, String>();
	private int expectedDLTime;
	private Date currentExecutionTime = null;
	
	public ClientTypeExecutionTimeAsgmtFileProcess(
			AppointedHaishinRuleConfig appointedHaishinRuleConfig) {
		this.appointedHaishinRuleConfig = appointedHaishinRuleConfig;
	}
	
	@Override
	void initial() throws AsgmtFileProcessInitialException {
		this.clientTypeExecutionTimeMap.clear();
		this.expectedDLTime = -1;
		this.currentExecutionTime = null;
		
		List<AppointedHaishinSizePattern> appointedHaishinSizePattern =
			this.appointedHaishinRuleConfig.getAppointedHaishinPatternList();
		for(AppointedHaishinSizePattern pattern : appointedHaishinSizePattern) {
			if ((super.targetFilesize > pattern.getLowerBound() && 
					super.targetFilesize <= pattern.getUpperBound())){
				this.expectedDLTime = pattern.getExpectedDLTime();
			}
		}
		if (this.expectedDLTime == -1) {
			throw new AsgmtFileProcessInitialException(
					"Can't find matched pattern for target filesize '" +
					super.targetFilesize + "'");
		}
	}

	@Override
	String getSpecificExecutionTimeContentInAsgmtFile(Date initialExecutionTime, BasicTerminalInfoBean basicTerminalInfoBean) {
		if (null == this.currentExecutionTime) {
			// cuurrentExecutionTime 為 Null 表示作過初始化動作
			this.currentExecutionTime = initialExecutionTime;
		}
		
		String clientType = basicTerminalInfoBean.getClientType();
		if (null==clientType || clientType.isEmpty()) {
			if (!this.clientTypeExecutionTimeMap.containsKey(EMPTY_CLIENT_TYPE_MAP_KEY)) {
				this.clientTypeExecutionTimeMap.put(
						EMPTY_CLIENT_TYPE_MAP_KEY,
						DateConvertUtils.dateToyyyyMMddHHmmString(this.currentExecutionTime));
				this.currentExecutionTime = DateUtils.addMinutes(this.currentExecutionTime, this.expectedDLTime);
			}
			return this.clientTypeExecutionTimeMap.get(EMPTY_CLIENT_TYPE_MAP_KEY);
		}
		else {
			if (!this.clientTypeExecutionTimeMap.containsKey(basicTerminalInfoBean.getClientType())) {
				this.clientTypeExecutionTimeMap.put(
						basicTerminalInfoBean.getClientType(),
						DateConvertUtils.dateToyyyyMMddHHmmString(this.currentExecutionTime));
				this.currentExecutionTime = DateUtils.addMinutes(this.currentExecutionTime, this.expectedDLTime);
			}
			return this.clientTypeExecutionTimeMap.get(basicTerminalInfoBean.getClientType());
		}
	}

}
